setwd("/Users/chuangchen/Library/CloudStorage/OneDrive-UniversityofPittsburgh/Pela project")

library(haven)
library(dplyr)
library(ggplot2)
library(ggpubr)


# robustness check plots
#install.packages("svglite")
library(svglite)

model_name <- list()
for(model.no in c(3,4,5,6,8,9)){
  model_name <- append(model_name, paste("model", model.no, "_coef", sep = ""))
}
print(model_name)


var_name <- list()

for(i in 1:9){
  var_name <- append(var_name, paste(i, ".eco_moral2", sep = ""))
}
var_name <- rep(var_name, 2) %>% as.data.frame()%>% t() %>% as.data.frame()
colnames(var_name) <- "var"
row.names(var_name) <- NULL
class(var_name)

name_index <- list("Model 3", "Model 4", "Model 5", "Model 6")

p_list <- list()
for(i in 1:4){
  m = model_name[i]
  name <- name_index[i]
  print(name)
  path = paste(m, ".csv", sep = "")
  print(path)
  model <- read.delim(path)
  names(model) <- c("var", "est")
  
  model$party_family <- NA
  pos_left <- which(model$var=="Far_Left")
  start <- pos_left+1
  end <- pos_left+27
  model$party_family[start:end] <- "far-left"
  pos_right <- which(model$var=="Far_Right")
  start <- pos_right+1
  end <- pos_right+27
  model$party_family[start:end] <- "far-right"
  model <- model[!is.na(model$party_family),]
  model$var[2] <- "ll"
  
  
  j <- 2
  k <- 3
  model$ll <- NA
  model$ul <- NA
  for(i in 1:18){
    model$var[j] <- "ll"
    model$ll[j-1]<- model$est[j]
    model$var[k] <- "ul"
    model$ul[k-2]<- model$est[k]
    j = j + 3
    k = k + 3
  }
  
  model <- model[!is.na(model$ll),]
  
  
  cust <- data.frame(
    term =  model$party_family,
    estimate = as.numeric(model$est),
    conf.low = as.numeric(model$ll),
    conf.high = as.numeric(model$ul),
    variable = var_name$var
  )
  
  plot_name <- paste(m, ".svg", sep = "")
  
  plot <- ggplot(cust, aes(y=factor(variable, levels = c("9.eco_moral2", "2.eco_moral2", "3.eco_moral2",
                                                 "4.eco_moral2", "5.eco_moral2", "6.eco_moral2",
                                                 "7.eco_moral2", "8.eco_moral2", "1.eco_moral2")), 
                   x=estimate, color = factor(term, levels = c("far-left", "far-right")))) +
    geom_pointrange(mapping=aes(xmin = conf.low, xmax = conf.high), 
                    position=position_dodge(-0.2)) +
    geom_vline(xintercept=0, linetype="dashed", color = "red") +
    scale_y_discrete(
      labels=c("1.eco_moral2" = "McEc", "2.eco_moral2" = "McEm", "3.eco_moral2" = "McEp",
               "4.eco_moral2" = "MmEc", "5.eco_moral2" = "MmEm", "6.eco_moral2" = "MmEp",
               "7.eco_moral2" = "MpEc", "8.eco_moral2" ="MpEm", "9.eco_moral2" = "MpEp")) +
    scale_x_discrete(breaks=c("Ec", "Em", "Ep"),
                     labels=c("conservative (Ec)", "middle (Em)", "progressive (Ep)")) +
    ylab("") +
    xlab("Coefficients") +
    scale_color_manual(values = c("Mc"="#FF99FF", "Mm"="chartreuse3", "Mp"="#0099FF"),
                       labels = c("conservative (Mc)", "middle (Mm)", "progressive (Mp)")) +
    guides(color=guide_legend(title="Party Family")) +
     ggtitle(name) +
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          panel.background = element_blank(), 
          axis.line = element_line(colour = "black"),
          axis.text.x = element_text(color = "black", size = 14),
          axis.text.y = element_text(color = "black", size = 14),
          axis.title.x = element_text(color = "black", size = 16),
          axis.title.y = element_text(color = "black", size = 16),
          axis.text = element_text(size = 20),
          plot.title = element_text(size = 25),
          legend.position = "bottom",
          legend.text = element_text(size = 14),
          legend.title = element_text(size = 16))
  ggsave(plot_name, plot, width=7, height=6)
  p_list <- append(p_list, plot)
}
  


p <- list()
#install.packages("cowplot")
#install.packages("magick")
library(cowplot)
library(magick)
for (i in 1:4){
  plot_name <- paste(model_name[i], ".svg", sep = "")
  p[[i]] <- ggdraw() + draw_image(plot_name)
  assign(plot_name, p[[i]])
}


plot_new <- plot_grid(plotlist=p, ncol=2, greedy = TRUE)
ggsave("robust_plot.pdf", plot_new, width=40, height=40, dpi=10000)



















######################################################

#install.packages("ggpubr")
library(ggpubr)
library(haven)
library(dplyr)
library(ggplot2)

name_index <- list("Model 3", "Model 4", "Model 5", "Model 6", "Model 8", "Model 9")
model_no <- list(3,4,5,6,8,9)

overall <- data.frame(left=c(0.2165, 0.2574, 0.2660, 0.2945, 0.2626, 0.3962), 
                      right=c(0.1584, 0.1427, 0.1194, 0.1135, 0.1170, 0.2084))


for(i in 1:6){
  m <- model_no[i]
  title <- name_index[i]
  print(title)
  
  for(j in c(1,3)){
    if(j==1){
      ylab = "Predicted Probability of Being Leftist"
      red_line = overall[i,1]
      red_line_lab = paste0("overall probability=", round(red_line, 2))
    }else{
      ylab = "Predicted Probability of Being Rightist"
      red_line = overall[i,2]
      red_line_lab = paste0("overall probability=", round(red_line, 2))
    }
    tmp_name <- paste("model", m, "_tmp", j, ".dta", sep = "")
    print(red_line)
    
    
    
    data1 <- read_dta(tmp_name) %>% select('_at1', '_margin', '_ci_lb', '_ci_ub')
    data1$eco <- c("Ec","Em","Ep","Ec","Em","Ep","Ec","Em","Ep")
    data1$moral <- c("Mc","Mc","Mc","Mm","Mm","Mm","Mp","Mp","Mp")
    colnames(data1) <- c("eco_moral", "pred", "ll", "ul", "eco", "moral")
    
    plot <- ggplot(data1, aes(x=eco, color = moral)) + 
      geom_vline(aes(xintercept="Ec"), linewidth=30, color = alpha("gray", 0.2)) +
      geom_vline(aes(xintercept="Em"), linewidth=30, color = alpha("gray", 0.2)) +
      geom_vline(aes(xintercept="Ep"), linewidth=30, color = alpha("gray", 0.2)) +
      geom_pointrange(aes(y=pred, ymin=ll, ymax=ul), position=position_dodge(0.5)) +
      theme(panel.grid.major = element_blank(), 
            panel.grid.minor = element_blank(),
            panel.background = element_blank(), 
            axis.line = element_line(colour = "black"),
            axis.text = element_text(size = 12),
            axis.title.y = element_text(size = 12),
            axis.title.x = element_text(size = 14),
            axis.text.x = element_text(color = "black"),
            axis.text.y = element_text(color = "black")) +
      ggtitle(title) +
      geom_hline(yintercept=as.numeric(red_line), linetype="dashed", color = "red") +
      annotate("text", x=j, y=as.numeric(red_line)+0.025, label=red_line_lab, size=3, color="red") +
      ylab(ylab) +
      xlab("Economic Position") +
      scale_x_discrete(breaks=c("Ec", "Em", "Ep"),
                       labels=c("conservative (Ec)", "middle (Em)", "progressive (Ep)")) +
      scale_y_continuous(limits = c(-0.1,1)) +
      scale_color_manual(values = c("Mc"="#FF99FF", "Mm"="chartreuse3", "Mp"="#0099FF"),
                         labels = c("conservative (Mc)", "middle (Mm)", "progressive (Mp)")) +
      guides(color=guide_legend(title="Moral Position")) + 
      theme(legend.position="bottom",
            legend.text = element_text(size=10),
            plot.title = element_text(size = 20))
    plot_name <- paste("model", m, "_outcome", j, sep = "")
    assign(plot_name, plot)
  }
  
}



plots1 <- ggarrange(model3_outcome1, model3_outcome3,
                    model4_outcome1, model4_outcome3,
                    model5_outcome1, model5_outcome3,
                    model6_outcome1, model6_outcome3,
                    ncol = 2, nrow = 4, common.legend = TRUE, heights = 1,
                    legend = "bottom") + bgcolor("white")
ggsave(filename = "left_right_model3_4_5_6.png", plots1, width=10, height=14, dpi=1000)


plots2 <- ggarrange(model8_outcome1, model8_outcome3,
                   model9_outcome1, model9_outcome3,
                   ncol = 2, nrow = 2, common.legend = TRUE, heights = 1,
                   legend = "bottom") + bgcolor("white")
ggsave(filename = "left_right_model8_9.png", plots2, width=10, height=8, dpi=1000)


